import qs from "querystring"
import url from "url"

export default function queryData(req,res,fn){
    if(req.method === "GET"){
        //获取 req所携带的 数据
        let {query} = url.parse(req.url)
        // 将数据转成对象
        let obj = qs.parse(query)
        //存放到 req中
        req.query = obj
        //调用回调函数
        fn()

    }else if(req.method === "POST"){
        let buf = Buffer.alloc(0)
        req.on("data",chunk=>{
            buf = Buffer.concat([buf,chunk])
        })
        req.on("end",()=>{
            buf = buf.toString()
            req.buf=buf
            fn()
            
        })
    }
}